using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb.GameObjects;

namespace BriansClimb.DrawableGameObjects.Buttons
{
    /// <summary>
    /// This is a game component that creates the toggle button
    /// </summary>
    public class ToggleButton : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Game game;
        protected GameTime toggledTime;
        protected MouseState mouseState;

        protected SpriteBatch spriteBatch;
        protected AudioManager audioManager;
        protected Texture2D initialToggleButton;
        protected Texture2D pressedToggleButton;

        protected string buttonType;
        protected bool toggled;
        protected bool pressed;
        protected int xCoord;
        protected int yCoord;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="game">Main Game Object</param>
        /// <param name="buttonName">Name specifying the toggle button type</param>
        /// <param name="x">Top left x-coordinate of the button texture</param>
        /// <param name="y">Top left y-coordinate of teh button texture</param>
        public ToggleButton(Game game, string buttonName, int x, int y)
            : base(game)
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            audioManager = (AudioManager)game.Services.GetService(typeof(AudioManager));

            this.game = game;
            
            buttonType = buttonName;
            xCoord = x;
            yCoord = y;
            toggled = false;

            LoadButtonTextures();
        }

        /// <summary>
        /// Returns the specific toggle button type.
        /// </summary>
        public string ButtonType
        {
            get { return buttonType; }
        }

        /// <summary>
        /// Returns if the toggle button is on/off.
        /// </summary>
        public bool Toggled
        {
            get { return toggled; }
            set { toggled = value; }
        }

        /// <summary>
        /// Returns/Sets the Time the Button was Toggled.
        /// </summary>
        public GameTime Time
        {
            get { return toggledTime; }
            set { toggledTime = value; }
        }

        /// <summary>
        /// Loads the toggle button textures based on the toggle button type created.
        /// </summary>
        private void LoadButtonTextures()
        {
            switch (buttonType)
            {
                case "rest":
                    initialToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleRest");
                    pressedToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleRestSelect");
                    break;
                case "jug":
                    initialToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleJug");
                    pressedToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleJugSelect");
                    break;
                case "footHold":
                    initialToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleFoot");
                    pressedToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleFootSelect");
                    break;
                case "crimp":
                    initialToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleCrimp");
                    pressedToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleCrimpSelect");
                    break;
                case "slope":
                    initialToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleSloper");
                    pressedToggleButton = game.Content.Load<Texture2D>(".\\Textures\\ToggleSloperSelect");
                    break;
            }
        }

        /// <summary>
        /// Checks for left mouse click(press & release) and changes the toggled state and the toggled time.
        /// </summary>
        /// <param name="timeIn">Snapshot of the timing values.</param>
        private void CheckToggle(GameTime timeIn)
        {
            mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed)
                pressed = true;
            if (mouseState.LeftButton == ButtonState.Released && pressed)
            {
                audioManager.PlayCue("btnclick7");
                toggled = !toggled;

                //checks the gametime and sets it as the time the button was toggled
                toggledTime = timeIn;
                pressed = false;                
            }
        }

        /// <summary>
        /// Checks if mouse is on the button location.
        /// </summary>
        /// <returns>True is on the button location, false otherwise.</returns>
        private bool MouseOnButton()
        {
            mouseState = Mouse.GetState();

            bool result = ((mouseState.X > xCoord) && (mouseState.X < (xCoord + initialToggleButton.Width)) &&
                            (mouseState.Y > yCoord) && (mouseState.Y < (yCoord + initialToggleButton.Height)));

            return result;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();

            if (MouseOnButton())
                CheckToggle(gameTime);
            

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            if(toggled)
                spriteBatch.Draw(pressedToggleButton,new Rectangle (xCoord, yCoord, 55, 55), Color.White);
            else
                spriteBatch.Draw(initialToggleButton,new Rectangle(xCoord, yCoord, 55, 55), Color.White);
            
            base.Draw(gameTime);
        }
    }
}